
package com.shop.cloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.mall.admin.service.UserGradeConfigService;
import com.shop.cloud.mall.common.entity.UserGradeConfig;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 用户等级配置
 *
 * @author 
 * @date 2023-03-15 14:18:57
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/usergradeconfig")
@Tag(description = "usergradeconfig", name = "用户等级配置管理")
public class UserGradeConfigController {

    private final UserGradeConfigService userGradeConfigService;

	/**
	 * list查询
	 * @param userGradeConfig
	 * @return
	 */
	@Operation(summary = "list查询")
	@GetMapping("/list")
	public R getList(UserGradeConfig userGradeConfig) {
		return R.ok(userGradeConfigService.list(Wrappers.query(userGradeConfig).lambda()
				.select(UserGradeConfig::getGrade,
						UserGradeConfig::getGradeName)));
	}

    /**
     * 用户等级配置分页列表
     * @param page 分页对象
     * @param userGradeConfig 用户等级配置
     * @return
     */
    @Operation(summary = "用户等级配置分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:usergradeconfig:index')")
    public R getPage(Page page, UserGradeConfig userGradeConfig) {
        return R.ok(userGradeConfigService.page(page, Wrappers.query(userGradeConfig)));
    }

    /**
     * 用户等级配置查询
     * @param id
     * @return R
     */
    @Operation(summary = "用户等级配置查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:usergradeconfig:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(userGradeConfigService.getById(id));
    }

    /**
     * 用户等级配置新增
     * @param userGradeConfig 用户等级配置
     * @return R
     */
    @Operation(summary = "用户等级配置新增")
    @SysLog("新增用户等级配置")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('mall:usergradeconfig:add')")
    public R save(@RequestBody UserGradeConfig userGradeConfig) {
		long count = userGradeConfigService.count();
		if(count <= 0){
			//第一次新增，保证级数从0开始
			userGradeConfig.setGrade(0);
			return R.ok(userGradeConfigService.save(userGradeConfig));
		}else{
			//查询前级是否存在，保证级数从0依次递增
			UserGradeConfig userGradeConfig2 = userGradeConfigService.getOne(Wrappers.<UserGradeConfig>query().lambda()
					.eq(UserGradeConfig::getGrade, userGradeConfig.getGrade()-1));
			if(userGradeConfig2 != null){
				if(userGradeConfig2.getDeblockingValue() >= userGradeConfig.getDeblockingValue()){
					return R.failed("解锁积分值必须大于前级");
				}
				return R.ok(userGradeConfigService.save(userGradeConfig));
			}else{
				return R.failed("等级数【" + userGradeConfig.getGrade() + "】不合法");
			}
		}
    }

    /**
     * 用户等级配置修改
     * @param userGradeConfig 用户等级配置
     * @return R
     */
    @Operation(summary = "用户等级配置修改")
    @SysLog("修改用户等级配置")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('mall:usergradeconfig:edit')")
    public R updateById(@RequestBody UserGradeConfig userGradeConfig) {
		UserGradeConfig userGradeConfig1 = userGradeConfigService.getById(userGradeConfig.getId());
		//查询前级是否存在
		UserGradeConfig userGradeConfig2 = userGradeConfigService.getOne(Wrappers.<UserGradeConfig>query().lambda()
				.eq(UserGradeConfig::getGrade, userGradeConfig.getGrade()-1));
		if(userGradeConfig2 != null){
			if(userGradeConfig2.getDeblockingValue() >= userGradeConfig1.getDeblockingValue()){
				return R.failed("解锁积分值必须大于前级");
			}
		}
		userGradeConfig.setGrade(userGradeConfig1.getGrade());
        return R.ok(userGradeConfigService.updateById(userGradeConfig));
    }

    /**
     * 用户等级配置删除
     * @param id
     * @return R
     */
    @Operation(summary = "用户等级配置删除")
    @SysLog("删除用户等级配置")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:usergradeconfig:del')")
    public R removeById(@PathVariable String id) {
    	//查询是否有更高级，有则不能删除
		UserGradeConfig userGradeConfig = userGradeConfigService.getById(id);
		UserGradeConfig userGradeConfig2 = userGradeConfigService.getOne(Wrappers.<UserGradeConfig>query().lambda()
				.eq(UserGradeConfig::getGrade, userGradeConfig.getGrade()+1));
		if(userGradeConfig2 != null){
			return R.failed("该等级有更高级【" + userGradeConfig2.getGradeName() + "】存在不能删除");
		}
        return R.ok(userGradeConfigService.remove(Wrappers.query(userGradeConfig)));
    }

}
